package com.hc.service.impl;

import com.hc.dao.IMenuDAO;
import com.hc.entity.UMenu;
import com.hc.service.IMenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class MenuServiceImpl implements IMenuService {

    @Resource
    IMenuDAO menuDAO;

    @Override
    public Set<UMenu> getAll() {
        Set<UMenu> menuList = menuDAO.selectAll();
        Map<Long, UMenu> menuMap = new HashMap<>();
        Set<UMenu> rootMenus = new HashSet<>();
        menuList.forEach(uMenu -> menuMap.put(uMenu.getMenuId(), uMenu));
        menuList.forEach(uMenu -> {
            Long pId = uMenu.getParentId();
            if (menuMap.containsKey(pId)) {
                menuMap.get(pId).getSubMenu().add(uMenu);
            } else {
                rootMenus.add(uMenu);
            }
        });
        return rootMenus;
    }
}
